﻿package net.htjs.mobile.android.crm;

import java.util.ArrayList;
import java.util.List;

import com.digiavenue.pulltorefresh.widget.PullToRefreshWebView;

import net.htjs.mobile.android.crm.util.L;
import android.app.Activity;
import android.graphics.Color;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainLayout {
	private Activity activity;
	private String [] contentItems=null;
	private ArrayList<PullToRefreshWebView> contentList=null;
	private ViewPager contentPager = null;
	private int contentIndex=0;
	private String []contentTitle=null;
	
	//构造函数
	public MainLayout(Activity activity , String []contentItems,String []contentTitle){
		this.activity = activity;
		this.contentPager=(ViewPager)activity.findViewById(R.id.content);
		this.contentItems = contentItems;
		this.contentList = new ArrayList<PullToRefreshWebView>();
		this.contentTitle=contentTitle;
		setContentPager();
		setContentIndex(0);
	}
	//设置滑动的内容
    public void setContentPager(){
		for(int i = 0;i < contentItems.length;i++){
			PullToRefreshWebView pwebView = new PullToRefreshWebView(activity);
//			WebView webView=pwebView.getRefreshableView();
//			webView.getSettings().setJavaScriptEnabled(true);
//			webView.setWebViewClient(new WebViewClient(){
//				 public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                     view.loadUrl(url);
//                     return true;
//             }
//			});
//			webView.setWebChromeClient(new WebChromeClient(){
//				public void onProgressChanged(WebView view, int progress){
//					activity.setTitle("加载中");
//					activity.setProgress(progress*100);
//					if(progress==100){
//						activity.setTitle(contentTitle[contentIndex]);
//					}
//				}
//			});
			contentList.add(pwebView);
		}
		contentPager.setAdapter(new SlideContentAdapter(contentList));//设置滑动内容适配器
		contentPager.setOnPageChangeListener(new SlideContentChangeListener());//设置滑动内容滑动监听器
	}
	
	public ViewPager getContentPager(){
		return this.contentPager;
	}
	
	//滑动滑动内容适配器
    class SlideContentAdapter extends PagerAdapter{
    	private List<PullToRefreshWebView> list;
    	
    	public SlideContentAdapter(List<PullToRefreshWebView> list){
    		this.list=list;
    	}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
		
		 @Override  
	    public int getItemPosition(Object object) {  
	        // TODO Auto-generated method stub  
	        return super.getItemPosition(object);  
	    }
		
		 @Override  
	    public void destroyItem(View arg0, int arg1, Object arg2) {  
	        // TODO Auto-generated method stub  
	        ((ViewPager) arg0).removeView(list.get(arg1));  
	    }  
	  
	    @Override  
	    public Object instantiateItem(View arg0, int arg1) {  
	        // TODO Auto-generated method stub  
	     ((ViewPager) arg0).addView(list.get(arg1));
	            
	        return list.get(arg1);  
	    }  
	  
	    @Override  
	    public void restoreState(Parcelable arg0, ClassLoader arg1) {  
	        // TODO Auto-generated method stub  
	  
	    }  
	  
	    @Override  
	    public Parcelable saveState() {  
	        // TODO Auto-generated method stub  
	        return null;  
	    }  
	  
	    @Override  
	    public void startUpdate(View arg0) {  
	        // TODO Auto-generated method stub  
	  
	    }  
	  
	    @Override  
	    public void finishUpdate(View arg0) {  
	        // TODO Auto-generated method stub  
	  
	    }   
    	
    }
    
	class SlideContentChangeListener implements OnPageChangeListener{

		public void onPageScrollStateChanged(int arg0) {
			
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
			if(arg0==1){
				Log.i("tag", "page scrolling");
			}
		}

		public void onPageSelected(int page) {
			contentIndex=page;
			MainLayout.this.setContentIndex(page);
			activity.setTitle(contentTitle[contentIndex]);
		}
	}
	
	public void setContentIndex(int contentIndex){
		PullToRefreshWebView pwebView = (PullToRefreshWebView)this.contentList.get(contentIndex);
		WebView webView=pwebView.getRefreshableView();
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebViewClient(new SampleWebViewClient());
		webView.loadUrl(this.contentItems[contentIndex]);
		activity.setTitle(contentTitle[contentIndex]);
	}
	
	private static class SampleWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}
	}
	
}
